$(function () {
    mui.init();
    var req = { //请求参数
        'gdid': localStorage.getItem('gdid'),
        'userid': localStorage.getItem('userid'),
        'token': localStorage.getItem('token')
    }
    var type = { //维保分类
        "1": {
            "names": "半月",
            "class": "half"
        },
        "2": {
            "names": "季度",
            "class": "quarter",
        },
        "3": {
            "names": "半年",
            "class": "half_year"
        },
        "4": {
            "names": "年度",
            "class": "year"
        }
    };
    var status = { //维保状态 可能做筛选
        "1": {
            "name": "通过",
            "class": ""
        },
        "2": {
            "name": "未通过",
            "class": "on"
        },
        "3": {
            "name": "无此项",
            "class": "on"
        }
    };
    mui.ready(function () {
        //加载框
        mui.showLoading("正在加载..", ".warn-list");
        Request();

    });
    //ajax请求处理数据
    function Request() {
        var url = domain + "/dtwbgl/fpcqwbdescitem.h5";
        $.ajax(url, {
            // data: JSON.stringify(req),
			data: req,
            dataType: 'json', //服务器返回json格式数据
            type: 'post', //HTTP请求类型
            timeout: 20000, //超时时间
            async: true, //true异步加载
			 headers: {
                'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
            },
            success: function (data) {
                if (data.code == 2001) {
                    //返回值处理
                    var ref = data.data;
                   
                    var wblist = ref; //具体维保项
                    var html = '';
										var wtghtml = '' ;//有问题的维保项
                    var total = 0; //总计
                    var jhtypeMax = 0; // 三目判断
                    console.log(ref);
                    wblist.forEach(function (e, i) {
                        // e是最外层
                        var item = e.item;
                        var typeName = type[e.jhtype].names;
                        var count = item.length;
                        jhtypeMax = (e.jhtype > jhtypeMax) ? e.jhtype : jhtypeMax;
                        total += count;
                        html += `<li><h2> ${typeName}维保项<span class="mui-pull-right"> ${count} 项</span></h2>`;
                        item.forEach(function (v, k) {
													
                           if(v.ifok!=''){
                                var ifokName = status[v.ifok].name;
																
                                var ifokClass = status[v.ifok].class;
                            }else{
                                var ifokName='';
                                var ifokClass = '';
                            }
                            if(v.ifok != 2){													
                                html += `
                                 <div class="mui-table">
                                     <div class="mui-table-cell mui-text-left">
                                         <p>
                                             <b>  </b> ${v.chkitem}
                                         </p>
                                     </div>
                                     <div class="mui-table-cell mui-text-right">
                                         <span class="${ifokClass}"> ${ifokName} </span>
                                     </div>
                                 </div>
                             `;
                             }else{
                                wtghtml+=`
                                <li>
                                     <h2>
                                         ${v.chkitem}<span class="mui-pull-right">${ifokName}</span>
                                     </h2>
                                     <p>
                                         反馈意见：<b>${v.reason}</b>
                                     </p>
                                 </li>
                                `;
                             }
                        });
                        html += `</li>`;
                    });
                    var typeName = type[jhtypeMax].names;
                    $(".repair-item-head h1 .total u").text(total);
                    $(".repair-item-head h1 b").text(typeName);
                    $(".repair-item-list").append(html);
										$(".wtg-list").append(wtghtml);



                    //隐藏加载框
                    mui.hideLoading(function () { //隐藏后的回调函数  
                    });
                } else if (data.code == 2002) {
                    mui.alert(data.msg);
                    mui.openWindow({
                        url: '../../login.html',
                        id: 'login.html'
                    })
                } else {
					mui.alert(data.msg);
                   mui.openWindow({
                   		url: '404.html',
                   		id: '404.html'
                   })
                }
            },
            error: function (xhr, type, errorThrown) { //异常处理； console.log(type);
               mui.alert(type);
               mui.openWindow({
               	url: '../../login.html',
               	id: 'login.html'
               })
            }
        });
    }

})